<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\admin\model\Project;
use app\admin\model\Admin;
/**
 *
 *
 * @icon fa fa-circle-o
 */
class AdminProject extends Backend
{

    /**
     * AdminProject模型对象
     * @var \app\admin\model\AdminProject
     */
    protected $model = null;
    protected $searchFields = "nickname|group_name";
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\AdminProject;
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function pmanager($ids = null) {
        $row = Project::get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $select_list = [];
        $group_arr = [GROUP_DISTRICTUNIT,GROUP_STREET,GROUP_LEADER];
        $admin_model = new Admin();
        $res1 = $admin_model->getUserList(GROUP_LEADER);
        $select_list[] = ['group_label'=>'领导组','data'=>$res1];
        $res2 = $admin_model->getUserList(GROUP_DISTRICTUNIT);
        $select_list[] = ['group_label'=>'区直部门','data'=>$res2];
        $res3 = $admin_model->getUserList(GROUP_STREET);
        $select_list[] = ['group_label'=>'街道','data'=>$res3];
        if ($this->request->isPost()) {
            $uids = $this->request->post("uid/a");
            $dataAll = [];
            foreach ($uids as $key => $value) {
                $groups = $this->auth->getGroups($value);
                $userinfo = $this->auth->getUserInfo($value);
                $data = [
                    'uid' => $value,
                    'nickname' => $userinfo['nickname'],
                    'project_pno' => $row['pno'],
                    'project_id' => $ids,
                    'group_id' => $groups[0]['id'],
                    'group_name' => $groups[0]['name'],
                ];
                $ad_row = $this->model->get(['uid'=>$value,'project_id'=>$ids]);
                if($ad_row)
                    $data['id'] = $ad_row['id'];
                $dataAll[] = $data;
            }
            $this->model->allowField(true)->saveAll($dataAll);
            $this->success();
        }
        $this->assign('select_list', $select_list);
        $this->assign('row', $row);
        $this->assignconfig('project_id', $ids);
        return $this->view->fetch();
    }

}
